package com.chuantou.yuba.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.chuantou.yuba.R;
import com.chuantou.yuba.util.Constants;

public class WebViewActivity extends BaseActionBarActivity {

	private WebView mWebView;
	private String mUrl;
	private String mTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.simple_webview);
		setupViews();
	}

	private void setupViews() {
		mWebView = (WebView) findViewById(R.id.webview);
		WebSettings settings = mWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		// settings.setUseWideViewPort(true);
		settings.setBuiltInZoomControls(false);
		mTitle = getIntent().getStringExtra(Constants.EXTRA_TITLE);
		setTitle(mTitle);
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (!TextUtils.isEmpty(url) && (url.startsWith("http://") || url.startsWith("https://"))) {
					view.loadUrl(url);
				}
				return true;
			}

		});
		mWebView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onReceivedTitle(WebView view, String title) {
				super.onReceivedTitle(view, title);
				if (TextUtils.isEmpty(mTitle)) {
					setTitle(title);
				}
			}
		});
		mUrl = getIntent().getStringExtra(Constants.EXTRA_URL);
		mWebView.loadUrl(mUrl);

	}

	@Override
	public void onBackPressed() {
		if (mWebView.canGoBack()) {
			mWebView.goBack();
		} else {
			super.onBackPressed();
		}
	}

}
